package util

import (
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
	"io"
)

// Base64Encode base64编码
func Base64Encode(str string) string {
	return base64.StdEncoding.EncodeToString([]byte(str))
}

// Base64Decode base64解码
func Base64Decode(base64str string) (string, error) {
	decodeBytes, err := base64.StdEncoding.DecodeString(base64str)
	return string(decodeBytes), err
}

// Sha256Encode sha256加密
func Sha256Encode(str string) (string, error) {
	w := sha256.New()
	// 将str写入到w中
	_, err := io.WriteString(w, str)
	if nil != err {
		return "", err
	}
	// 将w的hash转成[]byte格式
	bw := w.Sum(nil)
	// 将bw转成字符串
	return hex.EncodeToString(bw), nil
}
